package com.javaspeak.designpatterns.go4.structural.decorator;

/**
 * The Decorator pattern is useful when some data needs to be transformed
 * several times by different plugable transformers.
 * <p>
 * A Decorator can transform the data and then call another Decorator it is
 * chained to, to do some further processing on the data.  When the data has
 * been transformed by all the decorators it is returned.
 * <p>
 * In our example we have 2 decorators.  One decorator transforms the text to
 * upper case and the other decorator replaces multiple spaces with one space.
 * <p>
 * Each Decorator (transformer) holds a reference to another Decorator unless
 * it is the last Decorator in the chain.
 *
 * @author John Dickerson
 */
public class ApplicationDecorator {

    private Transformer<String> transformer;


    public void buildDecorators(){

        transformer =
            new TextCapitaliseTransformer();

        Transformer<String> removeMultipleSpacesTransformer =
            new RemoveMultipleSpacesTransformer();

        transformer.addDecoration(
                removeMultipleSpacesTransformer );

    }


    public void transform(){

        String message = "Hello    World!";

        System.out.println( this.transformer.transform( message ) );
    }


    /**
     * @param args
     */
    public static void main(String[] args) {

        ApplicationDecorator application = new ApplicationDecorator();
        application.buildDecorators();
        application.transform();
    }
}
